//
//  CampusListDescHeaderCell.m
//  ShowYork
//
//  Created by hxh on 2021/3/23.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "CampusDetailHeaderCell.h"
#import "CampusItemTypeFlowLayout.h"
#import "CampusDetailItemTypeCell.h"
#import "CampusParamItemModel.h"
#import "CampusConfigModel.h"
@interface CampusDetailHeaderCell ()<CampusItemTypeFlowLayoutDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIImageView *icon;
@property (nonatomic,strong)UILabel *mainTitleLb;
@property (nonatomic,strong)UILabel *subTitleLb;
@property (nonatomic,strong)UICollectionView *collectionView;

@end

@implementation CampusDetailHeaderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    self.icon = [[UIImageView alloc]init];
    self.icon.layer.cornerRadius = 8;
    self.icon.layer.masksToBounds = YES;
    self.icon.contentMode = UIViewContentModeScaleAspectFill;
    self.icon.backgroundColor = RGBA(240, 240, 240, 1);
    [self.contentView addSubview:self.icon];
    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-20);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    
    self.mainTitleLb = [[UILabel alloc]init];
    self.mainTitleLb.textColor = RGBA(58, 58, 58, 1);
    self.mainTitleLb.font = [UIFont boldSystemFontOfSize:22];
    self.mainTitleLb.numberOfLines = 2;
    [self.contentView addSubview:self.mainTitleLb];
    [self.mainTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(16);
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.icon.mas_left).offset(-20);
    }];
    
    self.subTitleLb = [[UILabel alloc]init];
    self.subTitleLb.textColor = RGBA(113, 113, 113, 1);
    self.subTitleLb.font = font(12);
    [self.contentView addSubview:self.subTitleLb];
    [self.subTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainTitleLb.mas_bottom).offset(16);
        make.left.equalTo(self.mainTitleLb);
    }];
    
    CampusItemTypeFlowLayout *layout = [CampusItemTypeFlowLayout new];
    layout.labelFont = font(12);
    //自定义layout初始化
    layout.delegate = self;
    layout.lineSpacing = 8.0;
    layout.interitemSpacing = 8.0f;
    layout.headerViewHeight = 0;
    layout.footerViewHeight = 0;
    layout.itemHeight = 16.0;
    layout.maxWidth = 140;
    layout.itemInset = UIEdgeInsetsZero;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.collectionView registerClass:[CampusDetailItemTypeCell class] forCellWithReuseIdentifier:@"CampusDetailItemTypeCell"];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.subTitleLb.mas_bottom);
        make.left.equalTo(self.mainTitleLb).offset(-8);
        make.right.equalTo(self.icon.mas_left).offset(-20);
        make.bottom.equalTo(self.contentView).offset(-12);
        make.height.mas_equalTo(@0).priorityHigh();
    }];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.hierarchyArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CampusDetailItemTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CampusDetailItemTypeCell" forIndexPath:indexPath];
    [cell setDetailItemType:self.model.hierarchyArray[indexPath.item]];
    return cell;
}

- (NSString *)collectionViewItemSizeWithIndexPath:(NSIndexPath *)indexPath{
    NSString *str = self.model.hierarchyArray[indexPath.item];
    return str;
}

+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"CampusDetailHeaderCell";
    CampusDetailHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[CampusDetailHeaderCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setModel:(CampusSchoolDescModel *)model{
    
    _model = model;

    self.mainTitleLb.text = model.name;
    
    [self.icon sd_setImageWithURL:[NSURL URLWithString:model.logo]];

    [self.collectionView reloadData];
    
    __block NSString *type = @"";
    CampusConfigModel *typeItem = [CampusConfigModel getWithKey:@"type"];
    [typeItem.paramList enumerateObjectsUsingBlock:^(CampusParamItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([model.type.stringValue isEqualToString:obj.name]) {
            type = obj.value;
            *stop = YES;
        }
    }];
    
    __block NSString *batch = @"";
    CampusConfigModel *batchItem = [CampusConfigModel getWithKey:@"batch"];
    [batchItem.paramList enumerateObjectsUsingBlock:^(CampusParamItemModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([model.batch.stringValue isEqualToString:obj.name]) {
            batch = obj.value;
            *stop = YES;
        }
    }];
    
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@(model.itemH)).priorityHigh();
    }];
    
    if (type.length != 0 && batch.length != 0) {
        self.subTitleLb.text = [NSString stringWithFormat:@"%@·%@·%@",model.city,batch,type];
    }else if (type.length != 0 && batch.length == 0){
        self.subTitleLb.text = [NSString stringWithFormat:@"%@·%@",model.city,type];
    }else if (type.length == 0 && batch.length != 0){
        self.subTitleLb.text = [NSString stringWithFormat:@"%@·%@",model.city,batch];
    }else{
        self.subTitleLb.text = model.city;
    }
    
}
@end
